/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月7日         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.coocaa.app.gamecenter.framework.ui.GameCenterBaseLayout;
import com.coocaa.app.gamecenter.framework.ui.GameCenterStatusBar;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;
import com.tianci.appstore.ui.widget.SkyCustomLayout.CustomLayoutParams;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class GameCenterLayout extends GameCenterBaseLayout{
    private SkyAppPager mPager = null;

    private SkyCustomLayout mCurrentLayout = null;

    private ImageView mDownArrow = null;
    private ImageView mTopShadow = null;
    private ImageView mBottomShadow = null;
    private ScrollView mCategoryScrollView = null;
    private ImageView downArrow = null;
    private static final String ALPHA_BG = "alpha_bg";

    public GameCenterLayout(Context context) {
        super(context);
        addFuncButtonLayout();
        addContentView();
        mPager.addView(getReCommendLayout());
        // addDownArrow()
        AsyncTask.runOnThread(new Runnable() {
            @Override
            public void run() {
                final int alpha = getAlphaBg();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        setBackgroundResource(alpha);
                    }
                });
            }
        });
        addTopShadow();
        addBottomShadow();
        addLogo();
        //mTopShadow.setVisibility(View.INVISIBLE);
        mStatusBar.bringToFront();
    }

    private int getAlphaBg() {
        String bg = "";
        try {
            bg = XServiceManager.getLiteConfigService().getStringValue(ALPHA_BG, "");
        } catch (RemoteException e) {
        }
        if (bg.equals("") || bg.equals(SkyConstants.GAME_CENTER_ALPHA_BG3)) {
            try {
                XServiceManager.getLiteConfigService().saveStringConfig(ALPHA_BG,
                        SkyConstants.GAME_CENTER_ALPHA_BG1);
            } catch (RemoteException e) {
            }
            return R.drawable.game_center_alpha_bg1;
        } else if (bg.equals(SkyConstants.GAME_CENTER_ALPHA_BG1)) {
            try {
                XServiceManager.getLiteConfigService().saveStringConfig(ALPHA_BG,
                        SkyConstants.GAME_CENTER_ALPHA_BG2);
            } catch (RemoteException e) {
            }
            return SkyConstants.getIconId(SkyConstants.GAME_CENTER_ALPHA_BG2);
        } else if (bg.equals(SkyConstants.GAME_CENTER_ALPHA_BG2)) {
            try {
                XServiceManager.getLiteConfigService().saveStringConfig(ALPHA_BG,
                        SkyConstants.GAME_CENTER_ALPHA_BG3);
            } catch (RemoteException e) {
            }
            return SkyConstants.getIconId(SkyConstants.GAME_CENTER_ALPHA_BG3);
        }
        return R.drawable.game_center_alpha_bg1;
    }

    public void showDownArrow(){
        if (downArrow == null){
            downArrow = new ImageView(mContext);
            downArrow.setImageResource(R.drawable.game_center_down_arrow);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
            params.bottomMargin = Div(20);
            addView(downArrow,params);

        }
        downArrow.setVisibility(View.VISIBLE);
    }

    public void hideDownArrow(){
        if (downArrow != null){
            downArrow.setVisibility(View.GONE);
        }
    }

    private void addLogo() {
        ImageView imageView = new CatchImageView(mContext);
        imageView.setBackgroundResource(R.drawable.game_center_logo);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(252), Div(75));
        params.topMargin = CoocaaApplication.Div(28);
        params.leftMargin = CoocaaApplication.Div(63);
        addView(imageView, params);
    }

    public void addCategoryLayout(CategoryData categoryData) {
        SkyCustomLayout layout = getCategoryLayout(categoryData);
        mCategoryScrollView = new ScrollView(mContext);
        mCategoryScrollView.setVerticalScrollBarEnabled(false);
        //mCategoryScrollView.setOnScrollEndListener(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT, Gravity.CENTER);
        mCategoryScrollView.addView(layout);
        mPager.addView(mCategoryScrollView, params);
    }



    public SkyAppPager getAppPager() {
        return mPager;
    }

    public GameCenterStatusBar getStatusBar() {
        return mStatusBar;
    }

    private void addContentView() {
        mPager = new SkyAppPager(mContext, Direction.VERTICAL);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT, Gravity.CENTER);
        addView(mPager, params);
    }


    private void addTopShadow(){
        mTopShadow = new ImageView(mContext);
        mTopShadow.setBackgroundResource(R.drawable.game_center_top_shadow);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL);
        addView(mTopShadow,params);
    }

    private void addBottomShadow(){
        mBottomShadow = new ImageView(mContext);
        mBottomShadow.setBackgroundResource(R.drawable.game_center_bottom_shadow);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
        addView(mBottomShadow,params);
    }


    private SkyCustomLayout getReCommendLayout() {
        SkyCustomLayout layout = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
        layout.setAnimation(R.anim.grid_anim);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
        params.topMargin = Div(127);
        layout.setLayoutParams(params);
        CustomLayoutParams netAppParams = new CustomLayoutParams();
        netAppParams.mRow = 5;
        netAppParams.mCol = 6;
        netAppParams.mBlockWidth = Div(243);
        netAppParams.mBlockHeight = Div(158);
        netAppParams.mVerticalSpace = 0;
        netAppParams.mHorizontalSpace = 0;
        netAppParams.mMarginLeft = Div(0);
        netAppParams.mMarginTop = Div(0);
        netAppParams.mBlockShadowX = Div(14);
        netAppParams.mBlockShdowY = Div(23);
        layout.setCustomLayoutParams(netAppParams);
        layout.setBlockDatas(generateNetAppBlockData());
        RecommendAdapter mAdapter = new RecommendAdapter(mContext);
        mAdapter.setmFocusChangedEvent(focusChangedEvent);
        layout.setAdapter(mAdapter);
        mCurrentLayout = layout;
        return layout;
    }

    private SkyCustomLayout getCategoryLayout(CategoryData categoryData) {
        SkyCustomLayout layout = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
        layout.setAnimation(R.anim.grid_anim);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);
        CustomLayoutParams netAppParams = new CustomLayoutParams();
        netAppParams.mRow = categoryData.mRow;
        netAppParams.mCol = categoryData.mColumn;
        netAppParams.mBlockWidth = Div(categoryData.mWidth);
        netAppParams.mBlockHeight = Div(categoryData.mHeight);
        netAppParams.mHorizontalSpace = Div(40);
        netAppParams.mVerticalSpace = Div(40);
        netAppParams.mBlockShadowX = Div(14);
        netAppParams.mBlockShdowY = Div(87);
        netAppParams.mMarginLeft = Div(0);
        netAppParams.mMarginTop = Div(0);
        layout.setCustomLayoutParams(netAppParams);
        layout.setBlockDatas(categoryData.mLayoutData);
        CategoryAdapter mAdapter = new CategoryAdapter(mContext);
        mAdapter.setmFocusChangedEvent(focusChangedEvent);
        layout.setAdapter(mAdapter);
        return layout;
    }

    public void initFocus() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LogUtils.d("focus", "init focus");
                if (mCurrentLayout.setFocusPos(0, true)) {
                    showFocusView();
                    openAnimation(true);
                }
            }
        }, 500);
    }

    private List<BlockData> generateNetAppBlockData() {
        List<BlockData> datas = new ArrayList<BlockData>();
        BlockData data1 = new BlockData();
        data1.mColNum = 3;
        data1.mRowNum = 3;
        data1.mBlock = new int[]{0, 1, 2, 6, 7, 8, 12, 13, 14};
        data1.mTurnPage = new boolean[]{false, false, false, false};
        datas.add(data1);

        BlockData data2 = new BlockData();
        data2.mColNum = 3;
        data2.mRowNum = 3;
        data2.mMarginLeft = Div(65);
        data2.mBlock = new int[]{3, 4, 5, 9, 10, 11, 15, 16, 17};
        data2.mTurnPage = new boolean[]{false, false, false, false};
        datas.add(data2);

        BlockData data3 = new BlockData();
        data3.mColNum = 2;
        data3.mRowNum = 2;
        data3.mMarginTop = Div(66);
        data3.mWidth = Div(481);
        data3.mBlock = new int[]{18, 19, 124, 25};
        data3.mTurnPage = new boolean[]{false, false, false, true};
        datas.add(data3);

        BlockData data4 = new BlockData();
        data4.mColNum = 2;
        data4.mRowNum = 2;
        data4.mMarginTop = Div(66);
        data4.mMarginLeft = Div(84);
        data4.mWidth = Div(481);
        data4.mBlock = new int[]{20, 21, 26, 27};
        data4.mTurnPage = new boolean[]{false, false, false, true};
        datas.add(data4);

        BlockData data5 = new BlockData();
        data5.mColNum = 2;
        data5.mRowNum = 2;
        data5.mMarginTop = Div(66);
        data5.mMarginLeft = Div(84);
        data5.mWidth = Div(481);
        data5.mBlock = new int[]{22, 23, 28, 29};
        data5.mTurnPage = new boolean[]{false, false, false, true};
        datas.add(data5);
        return datas;
    }

    @Override
    public View getContentView() {
        // TODO Auto-generated method stub
        return mPager;
    }

//    @Override
//    public void scrollEnd() {
//        if (mCategoryScrollView.isTop()){
//            LogUtils.d("scroll","is top");
//            //mTopShadow.setVisibility(View.INVISIBLE);
//            mBottomShadow.setVisibility(View.VISIBLE);
//        }else if(mCategoryScrollView.isBottom()){
//            LogUtils.d("scroll","is bottom");
//            mBottomShadow.setVisibility(View.INVISIBLE);
//            mTopShadow.setVisibility(View.VISIBLE);
//        }else{
//            LogUtils.d("scroll","is middle");
//            mBottomShadow.setVisibility(View.VISIBLE);
//            mTopShadow.setVisibility(View.VISIBLE);
//        }
//    }
}
